/* ***********************************************************
	MenuTest.c
	Joe Pardue Nov 10, 2008
	Revision: 1.00

	License: 
	This software is not suitable for any purpose whatsoever.
	The moment it is activated, it will destroy any machine
	it is run on and kill all life in a one kilometer radius . 
	Have a nice day!
*********************************************************** */

#include "MenuTest.h"


int main(void) 
{ 

// Setup the system
	initialization();

	// Introduce yourself
	show("You are talking to MenuTest.c 1.00\n\0");

	// Set the initial menu state
	menuState = 0;
	keyPush = 0;
	// Call the initial menu function
	menuState00Func();


// Loop forever
	while(1)
	{
		if(checkUART()) // a return of not 0 indicates a string is in s[]
		{
			parseUartInput();
		}

		if(checkJoystick()) // Process joystick state changes	
		{
			parseJoystickInput();
		}
	}

}



void parseUartInput()
{	
	switch (uartString[0])
	{
		case 'j':
			if( (uartString[1] == 'o') && (uartString[2] == 'y'))
			show("What joy?\r");
			uartCount = 0;// reset uartString array count to 0
			//joystick();
			break;
		case 'w':
			if( (uartString[1] == 'h') && (uartString[2] == 'o') && (uartString[3] == '?') )
			show("You are talking to the MenuTest.c.\r");
			uartCount = 0;// reset uartString array count to 0
			break;
		default:
			show("You sent: \"");
			sendUartRAMString(uartString);
			show("\" - I don't understand.\r");
			uartCount = 0;// reset uartString array count to 0
			break;
		
	}
	uartString[0] = '\0';
}

// You have new input from the joystick
// Call the current menu state function to find the next state
void parseJoystickInput()
{
	switch(menuState)
	{
		case 0:
			state00();
			break;
		case 1:
			state01();
			break;
		case 2:
			state02();
			break;		
		case 3:
			state03();
			break;
		case 4:
			state04();
			break;
		case 5:
			state05();
			break;			
		case 6:
			state06();
			break;
		case 7:
			state07();
			break;
		case 8:
			state08();
			break;			
		case 9:
			state09();
			break;
		case 10:
			state10();
			break;
		case 11:
			state11();
			break;	
		case 12:
			state12();
			break;
		case 13:
			state13();
			break;
		case 14:
			state14();
			break;				
		case 15:
			state15();
			break;
		case 16:
			state16();
			break;
		case 17:
			state17();
			break;						
		default:
			// TODO: DEAL WITH ERROR
			break;
	}
	
	// reset global variable so that we know the input is dealt with
	joystickInput = KEY_INVALID;

}

//
// state functions
//

// AVR BUTTERFLY state
void state00()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Do nothing
			break;
		case KEY_DOWN:
			// Change to state02
			menuState = 2;
			menuState02Func();
			break;
		case KEY_LEFT:
			// Do nothing
			break;
		case KEY_RIGHT:
			// Change to state01
			menuState = 1;
			menuState01Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState00Func();
			break;
		default:
		// Do nothing
			break;
	}
	joystickInput = KEY_INVALID;
}

// REVISION state
void state01()
{
	switch(joystickInput)
	{
		case KEY_UP:
			//  Do nothing
			break;
		case KEY_DOWN:
			//  Do nothing
			break;
		case KEY_LEFT:
			// Change to menu state00
			menuState = 0;
			menuState00Func();
			break;
		case KEY_RIGHT:
			// Do nothing
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState01Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

//  TIME state
void state02()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state11
			menuState = 00;
			menuState00Func();
			break;
		case KEY_DOWN:
			// Change to menu state11
			menuState = 11;
			menuState11Func();
			break;
		case KEY_LEFT:
			// Do nothing
			break;
		case KEY_RIGHT:
			// Change to menu state03
			menuState = 3;
			menuState03Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState02Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}


// CLOCK state
void state03()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// Change to menu state07
			menuState = 7;
			menuState07Func();
			break;
		case KEY_LEFT:
			// Change to menu state02
			menuState = 2;
			menuState02Func();
			break;
		case KEY_RIGHT:
			// Change to menu state04
			menuState = 4;
			menuState04Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState03Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}


// "HH:MM:SS" state
void state04()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			// Change to menu state03
			menuState = 3;
			menuState03Func();
			break;
		case KEY_RIGHT:
			// Change to menu state05
			menuState = 5;
			menuState05Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState04Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// ADJUST CLOCK state
void state05()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state06
			menuState = 6;
			menuState06Func();
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			// Change to menu state04
			menuState = 4;
			menuState04Func();
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState05Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// CLOCK FORMAT state
void state06()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// Change to menu state05
			menuState = 5;
			menuState05Func();
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState06Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// DATE state
void state07()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state03
			menuState = 3;
			menuState03Func();
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			// Change to menu state08
			menuState = 8;
			menuState08Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState07Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// "YY:MM:DD" state
void state08()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			// Change to menu state07
			menuState = 7;
			menuState07Func();
			break;
		case KEY_RIGHT:
			// Change to menu state09
			menuState = 9;
			menuState09Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState08Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// ADJUST DATE state
void state09()
{
	switch(joystickInput)
	{
		case KEY_UP:
			//
			break;
		case KEY_DOWN:
			// Change to menu state10
			menuState = 10;
			menuState10Func();
			break;
		case KEY_LEFT:
			// Change to menu state08
			menuState = 8;
			menuState08Func();
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState09Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// DATE FORMAT state
void state10()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state09
			menuState = 9;
			menuState09Func();
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState10Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// MUSIC state
void state11()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state02
			menuState = 2;
			menuState02Func();
			break;
		case KEY_DOWN:
			// Change to menu state15
			menuState = 15;
			menuState15Func();
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			// Change to menu state12
			menuState = 12;
			menuState12Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState11Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// FUR ELISE state
void state12()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// Change to menu state13
			menuState = 13;
			menuState13Func();
			break;
		case KEY_LEFT:
			// Change to menu state11
			menuState = 11;
			menuState11Func();
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState12Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// TURKEY MARCH state
void state13()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state12
			menuState = 12;
			menuState12Func();
			break;
		case KEY_DOWN:
			// Change to menu state14
			menuState = 14;
			menuState14Func();
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState13Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// SIREN state
void state14()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state13
			menuState = 13;
			menuState13Func();
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState14Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// NAME state
void state15()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// Change to menu state11
			menuState = 11;
			menuState11Func();
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			//
			break;
		case KEY_RIGHT:
			// Change to menu state16
			menuState = 16;
			menuState16Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState15Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// ENTER NAME state
void state16()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			// Change to menu state15
			menuState = 15;
			menuState15Func();
			break;
		case KEY_RIGHT:
			// Change to menu state17
			menuState = 17;
			menuState17Func();
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState16Func();
			break;
		default:
			// Do nothing
			break;
		}
		joystickInput = KEY_INVALID;
}

// DOWNLOAD NAME state
void state17()
{
	switch(joystickInput)
	{
		case KEY_UP:
			// 
			break;
		case KEY_DOWN:
			// 
			break;
		case KEY_LEFT:
			// Change to menu state16
			menuState = 16;
			menuState16Func();
			break;
		case KEY_RIGHT:
			//
			break;
		case KEY_PUSH:
			// Call this menu state funcion with keyPress = 1
			keyPush = 1;
			menuState17Func();
			break;
		default:
			//
			break;
		}
		joystickInput = KEY_INVALID;
}

//
// menuStateXXFunctons
//
void menuState00Func()
{
	if(keyPush)
	{
		show("state00 key push\r");
		keyPush = 0;
	}
	else
	{
		show("AVR BUTTERFLY\r");
	}
}

void menuState01Func()
{
	if(keyPush)
	{
		show("state01 key push\r");
		keyPush = 0;
	}
	else
	{
		show("REVISION\r");
	}
}

void menuState02Func()
{
	if(keyPush)
	{
		show("state02 key push\r");
		keyPush = 0;
	}
	else
	{	
		show("TIME\r");
	}
}

void menuState03Func()
{
	if(keyPush)
	{
		show("state03 key push\r");
		keyPush = 0;
	}
	else
	{
		show("CLOCK\r");
	}
}

void menuState04Func()
{
	if(keyPush)
	{
		show("state04 key push\r");
		keyPush = 0;
	}
	else
	{
		show("\"HH:MM:SS\"\r");
	}
}

void menuState05Func()
{
	if(keyPush)
	{
		show("state05 key push\r");
		keyPush = 0;
	}
	else
	{
		show("ADJUST CLOCK\r");
	}
}

void menuState06Func()
{
	if(keyPush)
	{
		show("state06 key push\r");
		keyPush = 0;
	}
	else
	{
		show("CLOCK FORMAT\r");
	}
}

void menuState07Func()
{
	if(keyPush)
	{
		show("state07 key push\r");
		keyPush = 0;
	}
	else
	{
		show("DATE\r");
	}
}

void menuState08Func()
{
	if(keyPush)
	{
		show("state08 key push\r");
		keyPush = 0;
	}
	else
	{
		show("\"YY:MM:DD\"\r");
	}
}

void menuState09Func()
{
	if(keyPush)
	{
		show("state09 key push\r");
		keyPush = 0;
	}
	else
	{
		show("ADJUST DATE\r");
	}
}

void menuState10Func()
{
	if(keyPush)
	{
		show("state10 key push\r");
		keyPush = 0;
	}
	else
	{
		show("DATE FORMAT\r");
	}
}

void menuState11Func()
{
	if(keyPush)
	{
		show("state11 key push\r");
		keyPush = 0;
	}
	else
	{
		show("MUSIC\r");
	}
}

void menuState12Func()
{
	if(keyPush)
	{
		show("state12 key push\r");
		keyPush = 0;
	}
	else
	{
		show("FUR ELISE\r");
	}
}

void menuState13Func()
{
	if(keyPush)
	{
		show("state13 key push\r");
		keyPush = 0;
	}
	else
	{
		show("TURKEY MARCH\r");
	}
}

void menuState14Func()
{
	if(keyPush)
	{
		show("state14 key push\r");
		keyPush = 0;
	}
	else
	{
		show("SIREN\r");
	}
}

void menuState15Func()
{
	if(keyPush)
	{
		show("state15 key push\r");
		keyPush = 0;
	}
	else
	{
		show("NAME\r");
	}
}

void menuState16Func()
{
	if(keyPush)
	{
		show("state16 key push\r");
		keyPush = 0;
	}
	else
	{
		show("ENTER NAME\r");
	}
}

void menuState17Func()
{
	if(keyPush)
	{
		show("state17 key push\r");
		keyPush = 0;
	}
	else
	{
		show("DOWNLOAD NAME\r");
	}
}

